package com.gw.vip.server.service.dao_service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.server.dao.TransMapper;
import com.gw.vip.server.po.HisTrans;
import com.gw.vip.server.po.Trans;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GW
 * @description
 * @since 2022-05-04
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class TransService extends ServiceImpl<TransMapper, Trans> {

    private final TransMapper mapper;
    private final HisTransService hisTransService;

    public void setClearDate(LocalDate clearDate, LocalDateTime clearMaxTime) {
        this.mapper.setClearDate(clearDate, clearMaxTime);
    }

    public void deleteById(String id) {
        if (StringUtils.isBlank(id)){
            log.error("id为空");
            throw new ServiceException("id为空");
        }
        this.mapper.deleteById(id);
    }

    public List<Trans> listByOrderId(String orderId) {
        List<Trans> transList = this.mapper.listByOrderId(orderId);
        if (transList.size()>0){
            return transList;
        }

        List<HisTrans> hisTransList = hisTransService.listByOrderId(orderId);
        transList = new ArrayList<>();
        for (HisTrans hisTrans : hisTransList){
            Trans trans = new Trans();
            BeanUtils.copyProperties(hisTrans, trans);
            transList.add(trans);
        }
        return transList;
    }
}
